#!/usr/bin/env python
# coding=utf-8
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, SelectField, TextAreaField, ValidationError, HiddenField, \
    BooleanField, PasswordField
from wtforms.validators import DataRequired, Email, Length, Optional, URL, Regexp, Required, EqualTo

from flask_wtf.file import FileField, FileAllowed, FileRequired
from fileshare.models import Admin


class LoginForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(1, 20)])
    password = PasswordField('密码', validators=[DataRequired(), Length(1, 128)])
    remember = BooleanField('记住我')
    submit = SubmitField('登录')


class RegistrationForm(FlaskForm):
    email = StringField('邮箱', validators=[Required(), Length(1, 64),
                                          Email()])
    username = StringField('用户名', validators=[
        Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                                          '用户名必须字母开头, '
                                          '包含数字，点或下划线')])
    password = PasswordField('密码', validators=[
        Required(), EqualTo('password2', message='两次密码输入不匹配')])
    password2 = PasswordField('确认密码', validators=[Required()])
    submit = SubmitField('注册帐号')

    def validate_email(self, field):
        if Admin.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已经注册')

    def validate_username(self, field):
        if Admin.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已被使用')


class LinkForm(FlaskForm):
    name = StringField('名称', validators=[DataRequired(), Length(1, 30)])
    url = StringField('链接', validators=[DataRequired(), URL(), Length(1, 255)])
    submit = SubmitField('提交')


class PasswordResetForm(FlaskForm):
    email = StringField('邮箱', validators=[Required(), Length(1, 64),
                                          Email()])
    password = PasswordField('新密码', validators=[
        Required(), EqualTo('password2', message='密码必须匹配')])
    password2 = PasswordField('确认密码', validators=[Required()])
    submit = SubmitField('重置密码')

    def validate_email(self, field):
        if Admin.query.filter_by(email=field.data).first() is None:
            raise ValidationError('未知的电子邮件地址')
